package com.xiaoshuidi.cloud.module.member.controller.app.user;


import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.AppUserInfoCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.AppUserInfoRespVO;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.AppUserUpdateMobileReqVO;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.MemberUpdateStateVO;
import com.xiaoshuidi.cloud.module.member.controller.app.user.vo.MemberUserStatisticsVO;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static com.xiaoshuidi.cloud.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;


@Tag(name = "租客APP - 用户个人中心")
@RestController
@RequestMapping(ApiConstants.NAME + "/user")
@Validated
public class AppUserController {

    @Resource
    private MemberUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建个人客户")
    public CommonResult<Boolean> create(@RequestBody AppUserInfoCreateVO vo) {
        userService.createUser(vo);
        return success(true);
    }


    @Operation(summary = "获取会员统计信息")
    @GetMapping("/member-statistics")
    public CommonResult<List<MemberUserStatisticsVO>> getMemberStatistics() {
        return CommonResult.success(userService.getMemberStatistics());
    }


//    @Operation(summary = "获取会员分页列表")
//    @PostMapping("/member-page")
//    public CommonResult<PageResult<MemberVO>> getMemberPage(@RequestBody MemberPageReqVO memberPageReqVo){
//        return CommonResult.success(userService.getMemberPage(memberPageReqVo));
//    }

//    @Operation(summary = "启用禁用会员账号")
//    @PostMapping("/member-update-state")
//    public CommonResult<Boolean> memberUpdateState(@RequestBody MemberUpdateStateVO memberUpdateStateVO){
//        return CommonResult.success(userService.memberUpdateState(memberUpdateStateVO));
//    }

    @PutMapping("/update-nickname")
    @Operation(summary = "修改用户昵称")
    public CommonResult<Boolean> updateUserNickname(@RequestParam("nickname") String nickname) {
        userService.updateUserNickname(getLoginUserId(), nickname);
        return success(true);
    }

    @PostMapping("/update-avatar")
    @Operation(summary = "修改用户头像")
    public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw exception(FILE_IS_EMPTY);
        }
        String avatar = userService.updateUserAvatar(getLoginUserId(), file.getInputStream());
        return success(avatar);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    public CommonResult<AppUserInfoRespVO> getUserInfo() {
        return success(userService.getUserInfoById(getLoginUserId()));
    }

    @PostMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    public CommonResult<Boolean> updateMobile(@RequestBody @Valid AppUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/edit")
    @Operation(summary = "edit")
    public CommonResult<Boolean> edit(@RequestBody AppUserInfoCreateVO vo) {
        userService.editUser(vo);
        return success(true);
    }

    @Operation(summary = "禁用会员账号")
    @PostMapping("/member-update-state")
    public CommonResult<Boolean> memberUpdateState(@RequestBody MemberUpdateStateVO memberUpdateStateVO) {
        return CommonResult.success(userService.memberUpdateState(memberUpdateStateVO));
    }

}

